/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using MyPhotoIndex.Utilities;
using MyPhotoIndex.Utilities.Types;
using MyPhotoIndex.Interfaces;
using MyPhotoIndex.ImageBank;
using System.Globalization;
using MyPhotoIndex.Interfaces.Tagging;
using MyPhotoIndex.Interfaces.Imaging;

namespace MyPhotoIndex.Controls.TagTree
{
    public partial class TagEdit : FormTemplate
    {
        private IImageTag m_tag;
        private TreeNode m_node;
        private String m_origKeyword;
        private int m_originalIndex;

        public TagEdit(IImageTag tag, TreeNode treeNode)
        {
            if (tag == null)
            {
                Logger.Log("tag was null");
                return;
            }

            InitializeComponent();

            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            m_tag = tag;

            txtName.Text = m_tag.Name;
            txtDescription.Text = m_tag.Description;

            if (m_tag.Thumbnail != null)
            {
                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                pictureBox1.Image = m_tag.Thumbnail;
            }

            txtKeyword.Text = m_tag.Keyword;
            m_origKeyword = m_tag.Keyword;

            dateTimePicker1.Value = m_tag.ReferenceDate;

            txtKeyword.TextChanged += new EventHandler(txtKeyword_TextChanged);
            txtName.TextChanged += new EventHandler(txtName_TextAlignChanged);

            selectTagIconButton.Click += new EventHandler(selectTagIconButton_Click);
            clearIconButton.Click += new EventHandler(clearIconButton_Click);

            if (tag.Parent == null)
            {
                txtKeyword.Visible = false;
                keywordLabel.Visible = false;

                dateTimePicker1.Visible = false;
                dateLabel.Visible = false;

                this.Height -= (txtKeyword.Height + dateTimePicker1.Height);
            }

            if (treeNode == null ||
                treeNode.Level == 0)
            {
                Spinner1.Visible = false;
                tagOrderLabel.Visible = false;
            }
            else
            {
                m_node = treeNode;
                m_originalIndex = m_node.Index;
                spinnerTextBox.Text = m_originalIndex.ToString(CultureInfo.InvariantCulture);

                upButton.Click += new EventHandler(upButton_Click);
                downButton.Click += new EventHandler(downButton_Click);

            }
        }

        void clearIconButton_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = null;
        }

        void selectTagIconButton_Click(object sender, EventArgs e)
        {
            BrowseForIcon();
        }

        void downButton_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.TreeView treeView = m_node.TreeView;
            TreeNode nodeParent = m_node.Parent;
            if (m_node == nodeParent.LastNode)
            {
                return;
            }

            treeView.BeginUpdate();
            treeView.Sorted = false;
            int index = m_node.Index;
            nodeParent.Nodes.Remove(m_node);
            nodeParent.Nodes.Insert(index + 1, m_node);
            treeView.SelectedNode = m_node;

            Win32Interop.SetTreeViewItemHeight(treeView, m_node, 2);
            treeView.Sorted = true;
            treeView.EndUpdate();

            spinnerTextBox.Text = m_node.Index.ToString(CultureInfo.InvariantCulture);
        }

        void upButton_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.TreeView treeView = m_node.TreeView; 
            TreeNode nodeParent = m_node.Parent;
            if (m_node == nodeParent.FirstNode)
            {
                return;
            }

            treeView.BeginUpdate();
            treeView.Sorted = false;
            int index = m_node.Index;
            nodeParent.Nodes.Remove(m_node);
            nodeParent.Nodes.Insert(index - 1, m_node);
            treeView.SelectedNode = m_node;

            Win32Interop.SetTreeViewItemHeight(treeView, m_node, 2);
            treeView.Sorted = true;
            treeView.EndUpdate();
         
            spinnerTextBox.Text = m_node.Index.ToString(CultureInfo.InvariantCulture);
        }

        void txtName_TextAlignChanged(object sender, EventArgs e)
        {
            ValidateFields();
        }

        void txtKeyword_TextChanged(object sender, EventArgs e)
        {
            ValidateFields();
        }

        private void ValidateFields()
        {
            bool valid = true;
            if (txtKeyword.Text.Trim().Length > 0 &&
                String.Compare(txtKeyword.Text, m_origKeyword, true, CultureInfo.InvariantCulture) != 0)
            {
                IImageTag tag = ImageTagCollection.Instance.GetTagByKeyword(txtKeyword.Text);
                if (tag != null)
                {
                    valid = false;
                }
            }

            if (valid == true)
            {
                if (txtName.Text.Trim().Length == 0)
                {
                    valid = false;
                }
            }

            base.OkButtonEnabled = valid;
        }

        protected override void OkButton_Click(object sender, EventArgs e)
        {
            if (m_tag.IsNameValid(txtName.Text) == false)
            {
                txtName.Focus();
                return;
            }

            m_tag.Name = txtName.Text;
            m_tag.Description = txtDescription.Text;
            m_tag.Thumbnail = pictureBox1.Image;
            m_tag.Keyword = txtKeyword.Text;
            m_tag.ReferenceDate = dateTimePicker1.Value;

            base.OkButton_Click(sender, e);
        }

        private void TagEdit_Load(object sender, EventArgs e)
        {
            Point formLocation = Cursor.Position;

            if (RightToLeft.Yes == this.RightToLeft)
            {
                formLocation.X -= this.Width;
            }

            this.Location = formLocation;
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            this.Validate();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            BrowseForIcon();
        }

        private void BrowseForIcon()
        {
            TagImageSelectionForm tisf = new TagImageSelectionForm(m_tag);
            if (tisf.ShowDialog(this) == DialogResult.OK)
            {
                IImageData imageData = tisf.SelectedImage;
                if (imageData != null)
                {
                    pictureBox1.Image = tisf.SelectedImage.Thumbnail;
                }
            }
        }

        public bool PositionChanged
        {
            get 
            {
                if (m_node == null)
                {
                    return false;
                }

                return m_originalIndex != m_node.Index; 
            }
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}